<?php

session_start();

require_once ("../../../db.php");


if (!isset($_SESSION['uid']) || ($_SESSION['type'] != 'student')) {
    die('Not logged in as an student user');
}

$sql = 'SELECT * FROM groupparticipants WHERE participantid=?';
$sth = $db->prepare($sql);
$sth->execute(array($_SESSION['uid']));     // select the group ID and check if user is in group
$group = $sth->fetch();
if (!$group) {
    echo json_encode(array('error' => 'Du er ikke i noen gruppe'));
} else {
    $sql = 'SELECT * FROM projectrequest WHERE groupid=? AND priority="taken"'; // select request that group have been selected
    $sth = $db->prepare($sql);
    $sth->execute(array($group[0]));
    $ourProject = $sth->fetchAll();

    $sql = 'UPDATE projects SET status="submitted" WHERE projectid=?';  // updates the project to submitted
    $sth = $db->prepare($sql);
    $sth->execute(array($ourProject[0]));

    $sql = 'DELETE FROM groupparticipants WHERE groupid=?';     // delete all groupmembers
    $sth = $db->prepare($sql);
    $sth->execute(array($group[0]));

    $sql = 'DELETE FROM projectrequest WHERE groupid=?';        // delete all projectrequest that the group have
    $sth = $db->prepare($sql);
    $sth->execute(array($group[0]));

    $sql = 'DELETE FROM projectgroups WHERE id=?';             // delete the group
    $sth = $db->prepare($sql);
    $sth->execute(array($group[0]));
}
?>